package com.bw.mallshop.mvvm.viewmodel;

import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.bw.common.entity.BaseEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.mallshop.entity.TokenEntity;
import com.bw.mallshop.mvvm.model.SplashModel;
import com.bw.mallshop.mvvm.view.FrameActivity;
import com.bw.mallshop.timmer.TimmerCallBack;
import com.bw.mallshop.timmer.TimmerHandler;
import com.bw.mallshop.timmer.TimmerThread;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;

public class SplashViewModel extends
        BaseViewModel<SplashModel> implements TimmerCallBack {

    public ObservableField<String>timeField=new ObservableField<>();

    private TimmerHandler handler;

    @Override
    protected void initData() {
        Map<String,Object>map=new HashMap<>();
        map.put("imie",""+System.currentTimeMillis());
        m.requestToken(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    protected void result(BaseEntity entity) {
        if (entity instanceof TokenEntity){
            TokenEntity tokenEntity = (TokenEntity) entity;
            //保存本地SP中
            SPUtils.getInstance().put("token",tokenEntity.getValues().token);
            handler = new TimmerHandler(this);
            new TimmerThread(handler).start();
        }
    }


    @Override
    public void timeCallBack(int time) {
        timeField.set(""+time);
        if (time == 1){
            startPage(FrameActivity.class);
            finishPage();
        }
    }
}
